<?php
    /* 
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    class Transport extends Admin_Controller {

        //put your code here
        function Transport() {
            parent::Admin_Controller();
            $this->load->model('Transport_m');
            $this->load->library('pagination');
            //$this->load->library('upload');
            $this->load->library('session');
            $this->load->helper('date');

            $this->_data['erreur'] = "";
        }

        function index() {
            $this->liste_transport();
        }
        function liste_transport() {
            is_admin();
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 7;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/transport/liste_transport/' . '/' . $per_page . '/';
            $config['per_page'] = '7';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->db->count_all('zoo_transporteur');
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['transport'] = $this->Transport_m->get_all_transport($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->db->count_all('zoo_transporteur');
            $this->_data['page_title'] = "Liste de transport";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('transport/liste_transport');
        }

        function ajouter_transport(){
            is_admin();
            $this->_data['page_title'] = "Update Transporteur";
            if ($this->input->post('submit')) {
                $formVal = array(
                array(
                'field' => 'nom',
                'Label' => 'Nom',
                'rules' => 'required'
                ),
                array(
                'field' => 'duree',
                'Label' => 'Durée',
                'rules' => 'required'
                )
                );
                $this->form_validation->set_rules($formVal);

                if ($this->form_validation->run() == TRUE) {
                    $data = array(
                    'nom' => $this->input->post('nom'),
                    'duree' => $this->input->post('duree')

                    );
                    $this->Transport_m->add_transport($data);
                    redirect('transport/liste_transport');
                } else {
                    $this->display_view('transport/add_transport');
                }
            } else {
                $this->display_view('transport/add_transport');
            }
        }
        function detail_transport($id){
            is_admin();
            $this->_data['page_title'] = "Détail Transporteur";
            $per_page = $this->uri->segment(4);
            if ($per_page == "") {
                $per_page = 7;
            }
            $off_set = $this->uri->segment(5);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/transport/detail_transport/' .$id. '/' . $per_page . '/';
            $config['per_page'] = '7';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Transport_m->count_tarif($id);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $detail_transport = $this->Transport_m->get_transport_by_id($id);
            $this->_data['detail_transport'] = $detail_transport;
            $this->_data['tarif_transport'] = $this->Transport_m->get_tarif_transport($id,$config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Transport_m->count_tarif($id);
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('transport/detail_transport');
        }
        function update_transport($id){
            is_admin();
            $this->_data['page_title'] = "Modifier Transporteur";
            $detail_transport = $this->Transport_m->get_transport_by_id($id);
            $this->_data['detail_transport'] = $detail_transport;
            if ($this->input->post('submit')) {
                $formVal = array(
                array(
                'field' => 'nom',
                'Label' => 'Nom',
                'rules' => 'required'
                ),
                array(
                'field' => 'duree',
                'Label' => 'Durée',
                'rules' => 'required'
                )
                );
                $this->form_validation->set_rules($formVal);

                if ($this->form_validation->run() == TRUE) {
                    $data = array(
                    'nom' => $this->input->post('nom'),
                    'duree' => $this->input->post('duree')

                    );
                    $this->Transport_m->update_transport($data,$id);
                    redirect('transport/liste_transport');
                } else {
                    $this->display_view('transport/update_transport');
                }
            } else {
                $this->display_view('transport/update_transport');
            }
        }
        function delete_transport($id, $per_page, $off_set) {
            is_admin();
            $this->Transport_m->delete_transport($id);
            redirect('transport/liste_transport' . '/' . $per_page . '/' . $off_set);
        }
        function ajouter_tarif($id)
        {
            is_admin();
            $detail_transport = $this->Transport_m->get_transport_by_id($id);
            $this->_data['detail_transport'] = $detail_transport;
            if ($this->input->post('submit')) {
                $formVal = array(
                array(
                'field' => 'de',
                'Label' => 'De',
                'rules' => 'required|numeric'
                ),
                array(
                'field' => 'max_tarif',
                'Label' => 'A',
                'rules' => 'required|numeric'
                ),
                array(
                'field' => 'tarif',
                'Label' => 'Tarif',
                'rules' => 'required|numeric'
                )
                );
                $this->form_validation->set_rules($formVal);
                if($this->form_validation->run()){
                    $data=array(
                    'min'=>$this->input->post('de'),
                    'id_transporteur'=>$id,
                    'tarif'=>$this->input->post('tarif'),
                    'max'=>$this->input->post('max_tarif'),
                    );
                    $this->Transport_m->add_tarif($data);
                    redirect('transport/detail_transport/'.$id);
                }
                else
                {
                    $this->display_view('transport/add_tarif');
                }
            }
            else{
                $this->display_view('transport/add_tarif');
            }
        }
        function delete_tarif($tranport_id,$id){
            is_admin();
            $this->Transport_m->delete_tarif($id);
            redirect('transport/detail_transport/'.$tranport_id);
        }
    }
?>
